package com.campus.merchant.controller;

import com.campus.common.bean.common.Pagination;
import com.campus.common.bean.common.Resp;
import com.campus.merchant.bean.order.OrderListQueryParam;
import com.campus.merchant.bean.order.OrderRejectBean;
import com.campus.merchant.service.OrderService;
import com.campus.merchant.vo.order.OrderDetailVO;
import com.campus.merchant.vo.order.OrderVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")

public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("分页查询商户订单列表")
    @GetMapping("/page")
    public Resp<Pagination<OrderVO>> page(@Validated OrderListQueryParam param) {
        PageInfo<OrderVO> pageInfo = orderService.getOrderListPage(param);
        return Resp.successPageable(pageInfo);
    }
    
    @ApiOperation("查询待处理订单数量")
    @GetMapping("/pending/count")
    public Resp<Long> getPendingOrderCount(@RequestParam Long merchantId) {
        Long count = orderService.getPendingOrderCount(merchantId);
        return Resp.success(count);
    }
    
    @ApiOperation("商户接单")
    @PostMapping("/accept/{orderId}")
    public Resp<String> acceptOrder(@PathVariable String orderId) {
        boolean result = orderService.acceptOrder(orderId);
        return result ? Resp.success("接单成功") : Resp.error("接单失败");
    }
    
    @ApiOperation("商户拒单")
    @PostMapping("/reject/{orderId}")
    public Resp<String> rejectOrder(@PathVariable String orderId, @RequestBody OrderRejectBean rejectBean) {
        boolean result = orderService.rejectOrder(orderId, rejectBean.getCancelReason());
        return result ? Resp.success("拒单成功") : Resp.error("拒单失败");
    }
    
    @ApiOperation("获取订单详情")
    @GetMapping("/detail/{orderId}")
    public Resp<OrderDetailVO> getOrderDetail(@PathVariable String orderId) {
        OrderDetailVO orderDetail = orderService.getOrderDetail(orderId);
        return Resp.success(orderDetail);
    }
    
    @ApiOperation("商户出餐")
    @PostMapping("/finish-meal/{orderId}")
    public Resp<String> finishMeal(@PathVariable String orderId) {
        boolean result = orderService.finishMeal(orderId);
        return result ? Resp.success("出餐成功") : Resp.error("出餐失败");
    }

}
